#include"headers.h"
#include"player.h"

/*
Player::Player(int Play_speed, string sheet, bool Main)
{
    if(Main) // if main character... as of player 1..
    {
        MAIN = true; // set it to true;
    }

    speed = Play_speed; // set the value speed is increased by..

    player_sheet = sheet; //sets the sting of where the bitmap is located

    Load_sprite_sheet();
}
*/

Player::~Player()
{
    destroy_bitmap(sprite);
    destroy_bitmap(sheet);
}
void Player::update()
{
    //handles key movements
    //key_movements();

   // AI_Avoid(int distance, int Target_x, int Target_y);
    //causes the AI to move away from opposing character.

    //handles frames
    Increase_Frame();
}

void Player::Draw(BITMAP *BUFFER)
{
    blit(sheet,sprite, X_coord * 32, 0, 0, 0,32,32);
    //destroy_bitmap(BUFFER);
    //blit(BUFFER,screen,0,0,0,0,SCREEN_W,SCREEN_H);
    draw_sprite(BUFFER, sprite, player_x,player_y);
}

void Player::Set_player_size(int s_h, int s_w)
{
    player_h = s_h; player_w = s_w;
}

void Player::Load_sprite_sheet()
{
    cout<<"Loading Player Bitmap.\n";
    sheet = load_bitmap(player_sheet.c_str(),0);

    if(!sheet)
    {
        cout<<"\a\a\aFailed to load: "<< player_sheet <<"\n";
    }
    else
    {
        cout<<"Loaded bitmap sucessfully: "<< player_sheet<<"\n";
    }

}
void Player::AI_Track(int Target_x, int Target_y)
{
    if(player_x > Target_x)
    {
        player_x-= speed;
    }
    if(player_x < Target_x)
    {
        player_x+= speed;
    }

    if(player_y > Target_y)
    {
        player_y-= speed;
    }
    if(player_y < Target_y)
    {
        player_y+= speed;
    }
}

void Player::AI_Avoid(int distance, int target_x, int target_y)
{
    bool x_axis = false;
    bool y_axis = false;

    if(player_y - distance < target_y && player_y + distance > target_y)
    //if in y's range of site...
    {
        y_axis = true;
    }

        if(player_x - distance < target_x && player_x + distance > target_x)
    {
        x_axis = true;
    }

    if(y_axis && x_axis)
    {
        if(player_y >= target_y)
        {
            player_y+= speed;
        }
        if(player_y < target_y)
        {
            player_y-= speed;
        }

        if(player_x >= target_x)
        {
            player_x+= speed;
        }
        if(player_x < target_x)
        {
            player_x-= speed;
        }

    }
}

void Player::key_movements()
{
    if(MAIN)
    {
        if(key[KEY_UP])
        {
            player_y--;
        }
        else if(key[KEY_LEFT])
        {
            player_x--;
        }
        else if(key[KEY_DOWN])
        {
            player_y++;
        }
        else if(key[KEY_RIGHT])
        {
            player_x++;
        }
    }
}

void Player::Increase_Frame()
{
    //
    frame += 0.5;
    if(frame > 60)
    {
        frame = 0;
    }
}

void Player::set_animation(bool value)
{
    animation = value;
}

void Player::Check_Frame(BITMAP *dest)
{

        masked_blit(sheet, dest, X_coord * 32, 0, player_x,player_y,32,32);

        blit(dest,screen,0,0,0,0,SCREEN_H,SCREEN_W);

}

int Player::get_x()
{
    return player_x;
}

int Player::get_y()
{
    return player_y;
}

    void Player::Set_play_speed(int player_speed)
    {
        speed = player_speed;
    }

    void Player::Set_sheet(string sheet)
    {
        player_sheet = sheet;

        Load_sprite_sheet();
    }

    void Player::set_main(bool Main)
    {

        if(Main) // if main character... as of player 1..
        {
            MAIN = true; // set it to true;
        }
    }


void Player::Set_sprite(int xcoord)
{
    if(xcoord > 6)
    {
        xcoord = 0;
    }
    X_coord = xcoord;
}

void Player::set_x(int s_x)
{
    player_x = s_x;
}

void Player::set_y(int s_y)
{
    player_y = s_y;
}
